/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.bettosimport.datos;

import com.bettosimport.entidades.Proveedor;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

/**
 *
 * @author Neil
 */
public class ProveedorDAO {

    private static ProveedorDAO _Instancia;

    private ProveedorDAO() {
    }

    public static ProveedorDAO Instancia() {
        if (_Instancia == null) {
            _Instancia = new ProveedorDAO();
        }
        return _Instancia;
    }

    public ArrayList<Proveedor> listarProveedores() throws Exception {
        ArrayList<Proveedor> proveedores;
        Connection cn = null;
        try {
            proveedores = new ArrayList<>();
            cn = ConexionDAO.Instancia().getConexion();
            CallableStatement cs = cn.prepareCall("{call sp_listarProveedor()}");
            ResultSet rs = cs.executeQuery();
            while (rs.next()) {
                Proveedor p = new Proveedor();
                p.setId_prov(rs.getInt("id_prov"));
                p.setNom_prov(rs.getString("nom_prov"));
                p.setRuc_prov(rs.getString("ruc_prov"));
                p.setDni_prov(rs.getString("dni_prov"));
                p.setDir_prov(rs.getString("dir_prov"));
                p.setTel_prov(rs.getString("tel_prov"));
                p.setEmail_prov(rs.getString("email_prov"));
                proveedores.add(p);
            }
            return proveedores;
        } catch (Exception e) {
            throw e;
        } finally {
            cn.close();
        }
    }

    public Proveedor buscarProveedor(String tipo, String documento) throws SQLException {
        Proveedor p = null;
        Connection cn = null;
        try {
            cn = ConexionDAO.Instancia().getConexion();
            CallableStatement cs = cn.prepareCall("{call sp_buscarProveedor(?,?)}");
            cs.setString(1, tipo);
            cs.setString(2, documento);
            ResultSet rs = cs.executeQuery();
            if (rs.next()) {
                p = new Proveedor();
                p.setId_prov(rs.getInt("id_prov"));
                p.setNom_prov(rs.getString("nom_prov"));
            }
        } catch (Exception e) {
        } finally {
            cn.close();
        }
        return p;
    }

}
